import { MediaPlayCore } from "./MediaPlayCore";
import { IMediaPlayCore } from "../../interfaces/IMediaPlayCore";
import { Log } from "../../../debug/Log";
import { MediaPlayEvent } from "../../events/MediaPlayEvent";
//class MP3PlayCore
    
    export  class MP3PlayCore extends MediaPlayCore implements IMediaPlayCore
    {
        constructor(arg1: string=null, arg2: number=1000, arg3: Boolean=false, arg4: number=250, arg5: Object=null)
        {
            super(url, bufferTime, callback);
            var url: string=null;
            var bufferTime: number=1000;
            var checkPolicyFile: Boolean=false;
            var updateDelay: number=250;
            var callback: Object=null;

            var loc1;
            url = arg1;
            bufferTime = arg2;
            checkPolicyFile = arg3;
            updateDelay = arg4;
            callback = arg5;
            this._timer = new Object /* flash.utils.Timer */(updateDelay);
            this._timer.addEventListener(flash.events.TimerEvent.TIMER, this.playingStateTimerEventHandler);
            this._context = new Object /* flash.media.SoundLoaderContext */(bufferTime, checkPolicyFile);
            this._sound = new Object /* flash.media.Sound */();
            this._sound.addEventListener(flash.events.IOErrorEvent.IO_ERROR, this.ioErrorEventHandler);
            this._sound.addEventListener(flash.events.Event.OPEN, this.openEventHandler);
            this._sound.addEventListener(flash.events.ProgressEvent.PROGRESS, this.progressEventHandler);
            this._sound.addEventListener(flash.events.Event.COMPLETE, this.completeEventHandler);
            this._sound.addEventListener(flash.events.Event.ID3, this.id3DataEventHandler);
            try 
            {
                this._sound.load(new Object /* flash.net.URLRequest */(url), this._context);
            }
            catch (err: Object /* flash.errors.IOError */)
            {
                Log.system("流加载IO错误：" + err.message);
            }
            catch (err)
            {
                Log.system("流加载安全性错误：" + err.message);
            }
            return;
        }

        public /* override */ set volume(arg1: number)
        {
            var loc1=null;
            super.volume = arg1;
            if (this._soundChanel) 
            {
                loc1 = this._soundChanel.soundTransform;
                loc1.volume = _mute ? 0 : arg1;
                this._soundChanel.soundTransform = loc1;
            }
            return;
        }

        public /* override */ play(): void
        {
            if (playing) 
            {
                Log.system("声音正处于播放状态，无法重复进行播放动作！");
            }
            else 
            {
                super.play();
                this.soundPlayAction();
            }
            return;
        }

        public /* override */ pause(): void
        {
            if (playing) 
            {
                super.pause();
                this.soundStopAction();
                callbackFunction(MediaPlayEvent.PAUSE);
            }
            else 
            {
                Log.system("声音正处于非播放状态，无法进行暂停播放动作！");
            }
            return;
        }

        public /* override */ resume(): void
        {
            if (paused) 
            {
                super.play();
                this.soundPlayAction(this._pauseTime);
                callbackFunction(MediaPlayEvent.RESUME);
            }
            else 
            {
                Log.system("声音正处于非暂停状态，无法进行暂停播放动作！");
            }
            return;
        }

        public /* override */ stop(): void
        {
            if (stopped) 
            {
                Log.system("声音正处于停止状态，无法重复进行停止播放动作！");
            }
            else 
            {
                super.stop();
                this.soundStopAction(true);
                callbackFunction(MediaPlayEvent.STOP);
            }
            return;
        }

        public /* override */ seek(arg1: number=0): void
        {
            var loc1=false;
            if (stopped) 
            {
                Log.system("声音正处于停止状态，无法进行播放进度搜索动作！");
            }
            else 
            {
                if (arg1 < 0) 
                {
                    arg1 = 0;
                }
                if (arg1 >= _totalTime) 
                {
                    arg1 = _totalTime - 0.001;
                }
                loc1 = playing;
                if (loc1) 
                {
                    this.pause();
                }
                this._pauseTime = arg1 * 1000;
                callbackFunction(MediaPlayEvent.SEEK);
                if (loc1) 
                {
                    this.resume();
                }
            }
            return;
        }

        public /* override */ seekPercent(arg1: number=0): void
        {
            var loc1=arg1 * _totalTime;
            this.seek(loc1);
            return;
        }

        public /* override */ dispose(): void
        {
            if (this._sound != null) 
            {
                this._sound.removeEventListener(flash.events.IOErrorEvent.IO_ERROR, this.ioErrorEventHandler);
                this._sound.removeEventListener(flash.events.Event.OPEN, this.openEventHandler);
                this._sound.removeEventListener(flash.events.ProgressEvent.PROGRESS, this.progressEventHandler);
                this._sound.removeEventListener(flash.events.Event.COMPLETE, this.completeEventHandler);
                this._sound.removeEventListener(flash.events.Event.ID3, this.id3DataEventHandler);
                this._sound = null;
            }
            if (this._timer != null) 
            {
                this._timer.removeEventListener(flash.events.TimerEvent.TIMER, this.playingStateTimerEventHandler);
                this._timer.stop();
                this._timer = null;
            }
            if (this._soundChanel != null) 
            {
                this._soundChanel.removeEventListener(flash.events.Event.SOUND_COMPLETE, this.soundCompleteEventHandler);
                this._soundChanel = null;
            }
            super.dispose();
            return;
        }

        /* internal  */soundPlayAction(arg1: number=0): void
        {
            if (!this._sound || !this._timer) 
            {
                return;
            }
            this._timer.start();
            this._soundChanel = this._sound.play(arg1);
            this._soundChanel.addEventListener(flash.events.Event.SOUND_COMPLETE, this.soundCompleteEventHandler);
            this.volume = _volume;
            return;
        }

        /* internal  */soundStopAction(arg1: Boolean=false): void
        {
            if (!this._timer || !this._soundChanel || !this._sound) 
            {
                return;
            }
            this._timer.stop();
            this._soundChanel.stop();
            this._soundChanel.removeEventListener(flash.events.Event.SOUND_COMPLETE, this.soundCompleteEventHandler);
            if (arg1) 
            {
                this._pauseTime = 0;
                try 
                {
                    this._sound.close();
                }
                catch (err: Object /* flash.errors.IOError */)
                {
                };
            }
            else 
            {
                this._pauseTime = this._soundChanel.position;
            }
            this._soundChanel = null;
            return;
        }

        /* internal  */ioErrorEventHandler(arg1: Object /* flash.events.IOErrorEvent */): void
        {
            callbackFunction(MediaPlayEvent.IO_ERROR, arg1.text);
            return;
        }

        /* internal  */openEventHandler(arg1: Object /* flash.events.Event */): void
        {
            if (!this._context) 
            {
                return;
            }
            var loc1=_totalTime * 1000;
            if (this._context.bufferTime > loc1) 
            {
                this._context.bufferTime = loc1;
            }
            callbackFunction(MediaPlayEvent.OPEN);
            return;
        }

        /* internal  */progressEventHandler(arg1: Object /* flash.events.ProgressEvent */): void
        {
            _loadPercentage = arg1.bytesLoaded / arg1.bytesTotal;
            callbackFunction(MediaPlayEvent.PROGRESS);
            return;
        }

        /* internal  */completeEventHandler(arg1: Object /* flash.events.Event */): void
        {
            callbackFunction(MediaPlayEvent.COMPLETE);
            return;
        }

        /* internal  */id3DataEventHandler(arg1: Object /* flash.events.Event */): void
        {
            if (!this._sound || !this._sound.id3) 
            {
                return;
            }
            _mediaData = this._sound.id3;
            _mediaData.type = "mp3";
            callbackFunction(MediaPlayEvent.INIT);
            return;
        }

        /* internal  */playingStateTimerEventHandler(arg1: Object /* flash.events.TimerEvent */): void
        {
            if (!this._sound || !this._soundChanel) 
            {
                return;
            }
            _buffering = this._sound.isBuffering;
            _playheadTime = this._soundChanel.position / 1000;
            _totalTime = this._sound.bytesTotal / (this._sound.bytesLoaded / this._sound.length) / 1000;
            if (_buffering) 
            {
                _bufferPercentage = this._sound.length / (this._soundChanel.position + _bufferTime);
                if (_bufferPercentage < 0) 
                {
                    _bufferPercentage = 0;
                }
                callbackFunction(MediaPlayEvent.BUFFERING);
            }
            else 
            {
                if (!this._isPlayStart && _playheadTime > 0) 
                {
                    this._isPlayStart = true;
                    callbackFunction(MediaPlayEvent.PLAY);
                }
                callbackFunction(MediaPlayEvent.PLAYING);
            }
            return;
        }

        /* internal  */soundCompleteEventHandler(arg1: Object /* flash.events.Event */): void
        {
            this.stop();
            callbackFunction(MediaPlayEvent.PLAY_COMPLETE);
            return;
        }

        private /* var */_sound: Object /* flash.media.Sound */;

        private /* var */_context: Object /* flash.media.SoundLoaderContext */;

        private /* var */_soundChanel: Object /* flash.media.SoundChannel */;

        private /* var */_timer: Object /* flash.utils.Timer */;

        private /* var */_pauseTime: number=0;

        private /* var */_isPlayStart: Boolean=false;
    }
